// pages/index/imgpage/imgpage.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:null,
    eid:null,
    title: "",
    pageIndex: 1,
    pageSize: 20,
    imgurl: app.globalData.imgurl,
    images:[],
    urls:[],
    keywords:"",
    stop:true,
    ispad:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var _this = this
    wx.getSystemInfo({
      success (res) {
        if(res.screenWidth>600){
          _this.setData({
            ispad:true
          })
        }else{
          _this.setData({
            ispad:false
          })
        }
      }
    })
    if(options.keywords){
      this.setData({
        keywords:options.keywords
      })
      this.search()
    }else{
      this.setData({
        id:options.id,
        eid:options.eid,
        title:options.name
      })
      
      wx.setNavigationBarTitle({
        title: options.name   
      }) 
      this.getImg()
    }
  },
  search(){
    var _this = this
    console.log(this.data.pageIndex);
    
    wx.request({
      url: app.globalData.baseurl +'/product/pictures?keywords='+ this.data.keywords,
      data: {
        pageIndex: this.data.pageIndex,
        pageSize: this.data.pageSize
      },
      method: "GET",
      success (res) {
        if(res.data.Items.length>0 && _this.data.stop){
          var arr = res.data.Items
          for(let i in arr){
            var imgarr = arr[i].ImageUrlThumbnail.split(";")
            if(_this.data.ispad){
              arr[i].smallImg = imgarr[3]
            }else{
              arr[i].smallImg = imgarr[3]
            }
          }
          _this.setData({
            images: _this.data.images.concat(arr)
          })
          if(res.data.Items.length==_this.data.pageSize){
            _this.setData({
              pageIndex: _this.data.pageIndex + 1
            })
          }else if(res.data.Items.length < _this.data.pageSize){
            _this.setData({
              stop: false
            })
          }
        }else{
          wx.showToast({
            title: '暂无更多',
            icon: 'none',
          })
        }
        // _this.setData({
        //   images: res.data.Items
        // })
      },
      fail(res){
        console.log(res)
      }
    })
    
  },
  getImg(){
    var _this = this
    var str = this.data.id + ',' + this.data.eid
    
    wx.request({
      url: app.globalData.baseurl +'/product/pictures?ids='+ str,
      data: {
        pageIndex: this.data.pageIndex,
        pageSize: this.data.pageSize
      },
      method: "GET",
      success (res) {
        if(res.data.Items.length>0 && _this.data.stop){
          var arr = res.data.Items
          for(let i in arr){
            var imgarr = arr[i].ImageUrlThumbnail.split(";")
            if(_this.data.ispad){
              arr[i].smallImg = imgarr[3]
            }else{
              arr[i].smallImg = imgarr[3]
            }
          }
          _this.setData({
            images: _this.data.images.concat(arr)
          })
          if(res.data.Items.length==_this.data.pageSize){
            _this.setData({
              pageIndex: _this.data.pageIndex + 1
            })
          }else if(res.data.Items.length < _this.data.pageSize){
            _this.setData({
              stop: false
            })
          }
        }else{
          wx.showToast({
            title: '暂无更多',
            icon: 'none',
          })
        }
        // _this.setData({
        //   images: res.data.Items
        // })
      },
      fail(res){
        console.log(res)
      }
    })
  },
  preview(e){
    var url = e.target.dataset.url
    var urls = []
    for(let i=0;i<this.data.images.length;i++){
      urls.push(this.data.imgurl + this.data.images[i].PictureAddress)
    }
    wx.previewImage({
      current: url, 
      urls: urls
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var _this = this
    // this.setData{
    //   pageIndex: this.data.pageIndex + 1
    // }
    if(this.data.keywords){
      this.search()
    }else{
      this.getImg()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '自定义转发标题',
      path: '/page/imgpage/imgpage?id=' + this.data.id + ',' + this.data.eid + "&name=" + this.data.title
    }
  }
})